﻿using GalaSoft.MvvmLight.Ioc;
using Tisbi.LawFirm.BL;
using Tisbi.LawFirm.BL.Services;
using Tisbi.LawFirm.DAL.Models;
using Tisbi.LawFirm.UI.Views.Controls.Forms;

namespace Tisbi.LawFirm.UI.ViewModel
{
    public class EmployessViewModel : BaseViewModel<Employees, IEmployeesServiceAgent>
    {
        private readonly CaseViewModel _caseViewModel;

        public EmployessViewModel() 
        {
            _caseViewModel = SimpleIoc.Default.GetInstance<CaseViewModel>();
        }

        protected override void ShowEditDialog()
        {
            if (!SecurityContextSingleton.GetInstance().IsRootLawyer())
            {
                return;
            }

            if (SelectedModelItem == null)
            {
                return;
            }

            var model = new EmployeeCRUDViewModal(SelectedModelItem);
            ShowEditDialog<EmployeesCRUDForm>(model, "Карточка изменения сотрудника");
        }

        protected override void ShowCreatedDialog()
        {
            if (!SecurityContextSingleton.GetInstance().IsRootLawyer())
            {
                return;
            }

            var newItem = new Employees();
            var detailModel = new EmployeeCRUDViewModal(newItem);

            ShowCreatedDialog<EmployeesCRUDForm>(detailModel, "Карточка создания нового сотрудника");
        }

        protected override void LoadModelBySearch()
        {
            IsBusy = true;

            ModelServiceAgent.GetItem
                (
                    (list, exception) =>
                        ModelLoadedByWhere(list, exception, item => item.ShortFio.Contains(SearchCriteria))
                );
        }

        protected override void SetCanProperties()
        {
            base.SetCanProperties();
            CanRemove = !IsBusy && SelectedModelItem != null && !ModelServiceAgent.HasCases(SelectedModelItem.Id);
            CanEdit = !IsBusy && SelectedModelItem != null;
        }

        protected override void Add(Employees employee)
        {
            base.Add(employee);
            _caseViewModel.LoadEmployees();
        }

        protected override void Remove()
        {
            if (!SecurityContextSingleton.GetInstance().IsRootLawyer())
            {
                return;
            }

            base.Remove();
            _caseViewModel.LoadEmployees();
        }

        protected override void Edit(Employees editItem)
        {
            base.Edit(editItem);
            _caseViewModel.LoadEmployees();
        }
    }
}
